package example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import org.pi4.locutil.Statistics;

public class ScoreNN {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String inputPath = "modelNN.txt", resultPath = "ScoreNN.txt", medianPath = "median.txt";
		int sampleId = 0;
		if (args.length > 0){
				inputPath = args[0];
		}
		if (args.length > 1){
			resultPath = args[1];
		}
		if (args.length > 3){
			medianPath = args[2];
			sampleId = Integer.parseInt(args[3]);
		}
		
		//Construct parsers
		File inputFile = new File(inputPath);
		ResultParser inputParser = new ResultParser(inputFile);
		System.out.println("ResultParser File: " + inputFile.getAbsoluteFile());
		
		try {
			ArrayList<Double> distances = inputParser.parse();
			
			//create outputfile
			FileWriter fstream = new FileWriter(resultPath);
			BufferedWriter out = new BufferedWriter(fstream);
			//create output file for median data
			FileWriter fstream2 = new FileWriter(medianPath, true);
			BufferedWriter out2 = new BufferedWriter(fstream2);
			
			Collections.sort(distances);
			Double median = Statistics.median(distances);
			out2.append(sampleId + ";" + median.toString() + '\n');
			for(Double entry: distances) {
				out.write(entry.toString() + "\n");
				System.out.println(entry.toString());
			}
			out2.close();
			out.close();
			System.out.println("Done analyzing " + distances.size() + " entries.");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
